package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.domain.Division;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.UserlistService;
import com.example.mybatisplus.model.domain.Userlist;

import javax.servlet.http.HttpSession;


//前端控制器
@Controller
@RequestMapping("api/userlist")
public class UserlistController {

    private final Logger logger = LoggerFactory.getLogger(UserlistController.class);

    @Autowired
    private UserlistService userlistService;

//    登录
    @PostMapping("login")
    @ResponseBody
    public JsonResponse login(@RequestBody Userlist userlist) {
        Userlist login = userlistService.login(userlist);
        //保存当前登录用户 session(保存在服务端) 以键值对的方式 日志
        //只有成功登录才能保存
        if (login != null) {
            //保存
            SessionUtils.saveCurUser(login);
//            Userlist curUser = SessionUtils.getCurUser();
        }
        return JsonResponse.success(login);
    }

//    退出登录
    @PostMapping("logout")
    @ResponseBody
    public JsonResponse logout() {
//      从seesion中移除数据
        SessionUtils.removeCurUser();
        return JsonResponse.success("成功");
    }


//    修改密码
    @PostMapping("updateById")
    @ResponseBody
    public JsonResponse updateById(@RequestBody Userlist userlist) {
        boolean b = userlistService.updateById(userlist);
        return JsonResponse.success(b);
    }


}

